package com.situ.spring.test;

import com.situ.spring.controller.BanjiController;
import com.situ.spring.controller.CourseController;
import com.situ.spring.controller.StudentController;
import com.situ.spring.entity.Student;
import com.situ.spring.util.JDBCUtil;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo {
    public static void main(String[] args) {
        // Student student = new Student();

        // 创建Spring容器，在XML中创建的bean都会new出对象放到Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从Spring容器中根据名字取出new出来的对象
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }

    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student1 = (Student) context.getBean("student");
        Student student2 = (Student) context.getBean("student");
        System.out.println(student1 == student2);
    }

    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentController studentController = (StudentController) context.getBean("studentController");
        studentController.selectAll();
    }

    @Test
    public void test3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        CourseController courseController = (CourseController) context.getBean("courseController");
        courseController.selectAll();
    }

    @Test
    public void test4() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BanjiController banjiController = (BanjiController) context.getBean("banjiController");
        banjiController.selectAll();
    }

    @Test
    public void test5() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql1 = "UPDATE account SET money=money-1000 WHERE name='张三'";
        String sql2 = "UPDATE account SET money=money+1000 WHERE name='李四'";
        try {
            connection = JDBCUtil.getConnection();
            // AutoCommit默认是true
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql1);
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();

            int i = 3 / 0;

            preparedStatement = connection.prepareStatement(sql2);
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();

             connection.commit();
        } catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }

    }

}
